package org.example;

public class Test2 {
    public int longestCommonSubsequence(String text1, String text2) {
        char[] arr1 = text1.toCharArray(), arr2 = text2.toCharArray();
        int n1 = arr1.length, n2 = arr2.length;
        //dp[i][j]表示test1以i位置为结尾的子序列和text2以j位置为结尾的子序列的最长公共子序列
        int[][] dp = new int[n1+1][n2+1];
        for (int i = 1; i <= n1; i++) {
            for (int j = 1; j <= n2; j++) {
                if (arr1[i-1] == arr2[j-1]) {
                    //当arr[i-1] == arr2[j-1]的时候dp[i][j] = dp[i-1][j-1] + 1
                    dp[i][j] = dp[i-1][j-1] + 1;
                }else {
                    //当arr[i-1] != arr2[j-1]的时候arr1[i-1]可能=arr2[j],arr1[i]可能等于arr2[j-1]
                    //所以dp[i][j]为Math.max(dp[i][j-1],dp[i-1][j])
                    dp[i][j] = Math.max(dp[i-1][j],dp[i][j-1]);
                }
            }
        }
        return dp[n1][n2];
    }

    public static void main(String[] args) {
        String s1 = "abcde",s2 = "ace";
        Test2 test2 = new Test2();
        test2.longestCommonSubsequence(s1,s2);
        System.out.println(666);
    }
}
